<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        .search-btn {
            margin-left: 20px;
        }

        .tableBar {
            justify-content: flex-start !important;
        }

        .info-box {
            margin: -15px -44px 20px;
        }

        .info-box .item-box {
            display: flex;
            height: 20px;
            line-height: 20px;
            font-size: 14px;
            font-weight: 400;
            color: #666666;
            text-align: left;
            margin-bottom: 14px;
        }

        .info-box .item-box:last-child {
            margin-bottom: 0;
        }

        .info-box .item-box .label {
            width: 96px;
        }

        .info-box .item-box .des {
            flex: 1;
            color: #333333;
            width: auto;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="warehouse-app" v-loading="loading">
    <div class="container">
        <!-- 搜索项 -->
        <div class="tableBar">
            <el-input v-model="drugId" placeholder="请输入药品编号" style="width: 250px">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>&nbsp;&nbsp;&nbsp;
            <el-input v-model="purchaseId" placeholder="请输入进货单号" style="width: 250px">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>&nbsp;&nbsp;&nbsp;
            <el-input v-model="manufacturerId" placeholder="请输入产商编号" style="width: 250px">
                <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="init"></i>
            </el-input>
            <el-button type="primary" class="search-btn" @click="init">查询</el-button>
        </div>

        <el-table :data="tableData" stripe class="tableBox">
            <el-table-column prop="drugId" label="药品编号" show-overflow-tooltip></el-table-column>
            <el-table-column prop="purchaseId" label="进货单号" show-overflow-tooltip></el-table-column>
            <el-table-column prop="manufacturerId" label="产商编号" show-overflow-tooltip></el-table-column>
            <el-table-column prop="stock" label="库存" min-width="40px"></el-table-column>
            <el-table-column prop="salePrice" label="销售价格" min-width="55px">
                <template slot-scope="{ row }">
                    <span>￥{{ row.salePrice }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="销售状态" min-width="40px">
                <template slot-scope="{ row }">
                    <span>{{ getSaleStatus(row) }}</span>
                </template>
            </el-table-column>

            <el-table-column prop="sort" label="排序" min-width="40px"></el-table-column>

            <el-table-column
                    prop="updateTime"
                    label="操作时间"
                    show-overflow-tooltip>
            </el-table-column>

            <el-table-column prop="btn" label="操作">
                <template slot-scope="{ row }">
                    <el-button type="text" @click="showDrugHandle(row)" class="blueBug">
                        药品信息
                    </el-button>
                    <el-divider v-if="row.status === 1" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 1" type="text" @click="updateWarehouseStatus(0, row.id)"
                               class="blueBug">
                        停售
                    </el-button>
                    <el-divider v-if="row.status === 0" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 0" type="text" @click="updateWarehouseStatus(1, row.id)"
                               class="delBut non">
                        起售
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="showUpdateWarehouseHandle(row)">
                        编辑
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                :current-page.sync="page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>


    <el-dialog title="药品信息" :visible.sync="dialogTableVisible">
        <el-form ref="drugData" :model="drugData,purchaseDetailData" label-width="80px">
            <el-form-item label="药品名称:">
                <el-input v-model="drugData.name" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="药品编号:">
                <el-input v-model="drugData.id" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="进货单价:">
                <el-input v-model="purchaseDetailData.purchasePrice" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="进货数量:">
                <el-input v-model="purchaseDetailData.purchaseCount" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="生产日期:">
                <el-input v-model="purchaseDetailData.produceDate" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="有效日期:">
                <el-input v-model="purchaseDetailData.effectiveDate" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="药品图片:">
                <el-image style="width: auto; height: auto; border:none;cursor: pointer;"
                          :src="getImage(drugData.image)"
                          :preview-src-list="[ `/common/download?name=${drugData.image}` ]">
                    <div slot="error" class="image-slot">
                        <img src="./../../images/404-images/404-cloud.png"
                             style="width: auto; height: 40px; border:none;">
                    </div>
                </el-image>
            </el-form-item>

            <el-form-item label="计量单位:">
                <el-input v-model="drugData.unit" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="药品类型:">
                <el-input v-model="drugData.type" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="药品功效:">
                <el-input type="textarea" v-model="drugData.efficacy" disabled="disabled"></el-input>
            </el-form-item>
        </el-form>
    </el-dialog>


    <el-dialog
            :title="classData.title"
            :visible.sync="classData.dialogVisible"
            width="30%"
            :before-close="handleClose2">
        <el-form
                class="demo-form-inline"
                label-width="100px">
            <el-form-item
                    label="销售单价(元):"
                    prop="salePrice">
                <el-input
                        v-model="classData.salePrice"
                        placeholder="请输入药品销售单价"
                        maxlength="20"/>
            </el-form-item>

            <el-form-item
                    label="排序:"
                    prop="sort">
                <el-input
                        v-model="classData.sort"
                        placeholder="请输入排序，数值越大，药品展示越靠前"
                        maxlength="20"/>
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer">
        <el-button
                size="medium"
                @click="classData.dialogVisible = false"
        >取 消</el-button>
        <el-button
                type="primary"
                size="medium"
                @click="updateWarehouseHandle()"
        >确 定</el-button>
      </span>
    </el-dialog>

</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/warehouse.js"></script>
<script src="../../api/drug.js"></script>
<script src="../../api/purchaseDetail.js"></script>
<script src="../../api/category.js"></script>
<script>
    new Vue({
        el: '#warehouse-app',
        data() {
            return {
                purchaseId: '',
                manufacturerId: '',
                drugId: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                loading: false,
                dialogTableVisible: false,
                drugData: {},
                purchaseDetailData: {},
                typeList:[],
                classData: {
                    'title': '更新药品信息',
                    'dialogVisible': false,
                    'id': '',
                    'salePrice': '',
                    'sort': '',
                },
            }
        },
        computed: {},
        created() {
            this.init()
        },
        mounted() {
            // this.loading = false
        },
        methods: {
            async init() {
                this.getTypeList()
                getWarehousePage({
                    page: this.page,
                    pageSize: this.pageSize,
                    drugId: this.drugId || undefined,
                    purchaseId: this.purchaseId || undefined,
                    manufacturerId: this.manufacturerId || undefined,
                }).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            getSaleStatus(row) {
                let str = ''
                switch (row.status) {
                    case 1:
                        str = '起售中'
                        break;
                    default:
                        str = '停售中'
                }
                return str
            },

            updateWarehouseStatus(status, id) {
                const CountReg = /^[1-9]{1,2}|0$/
                if (!CountReg.test(status)) {
                    this.$message.error('操作失败')
                    return
                }
                this.$confirm('确认更改该药品库存状态?', '提示', {
                    'confirmButtonText': '确定',
                    'cancelButtonText': '取消',
                    'type': 'warning'
                }).then(() => {
                    updateWarehouse({'id': id,'status': status}).then(res => {
                        if (res.code === 1) {
                            this.$message.success(status === 1 ? '药品已起售' : '药品已停售')
                            this.init()
                        } else {
                            this.$message.error(res.msg || '操作失败')
                        }
                    }).catch(err => {
                        this.$message.error('请求出错了：' + err)
                    })
                })
            },
            showUpdateWarehouseHandle(row) {
                this.classData.dialogVisible = true
                this.classData.id = row.id
                this.classData.salePrice = row.salePrice
                this.classData.sort = row.sort
            },
            showDrugHandle(data) {
                this.dialogTableVisible = true
                this.drugData = {}
                this.purchaseDetailData = {}
                queryDrugById(data.drugId).then(res => {
                    if (String(res.code) === '1') {
                        this.drugData = res.data
                        this.drugData.unit = this.drugData.unit == '0' ? '瓶' : this.drugData.unit == '1' ? '支' : '袋'
                        for (let type of this.typeList) {
                            if (res.data.type==type.id) {
                                res.data.type=type.name
                                break
                            }
                        }
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })

                queryPurchaseDetailById(data.purchaseDetailId).then(res => {
                    if (String(res.code) === '1') {
                        this.purchaseDetailData = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })

            },
            updateWarehouseHandle() {
                const classData = this.classData
                const priceReg = /^(([1-9]\d{0,5})|0)(\.\d{1,2})?$/
                const CountReg = /^[1-9]{1,5}|0$/
                if (classData.salePrice!=''&&!priceReg.test(classData.salePrice)) {
                    this.$message.error('药品价格格式错误（整数位不能超过5位，小数位不能超过2位）')
                    return
                }
                if (classData.sort!=''&&!CountReg.test(classData.sort)) {
                    this.$message.error('排序格式错误（整数位不能超过5位）')
                    return
                }
                updateWarehouse({
                    'id': classData.id,
                    'salePrice': classData.salePrice,
                    'sort': classData.sort,
                }).then(res => {
                    console.log(res)
                    if (res.code === 1) {
                        this.$message.success('库存信息修改成功！')
                        this.classData.dialogVisible = false
                        this.classData.id = ''
                        this.classData.salePrice = ''
                        this.classData.sort = ''
                        this.handleQuery()
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            getImage(image) {
                return `/common/download?name=${image}`
            }
            ,
            handleClose() {
                this.dialogVisible = false
            }
            ,
            handleClose2() {
                this.classData.dialogVisible = false
            }
            ,
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            }
            ,
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
            getTypeList() {
                queryCategoryList().then(res => {
                    if (res.code === 1) {
                        this.typeList = res.data
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
        }
    })
</script>
</body>
</html>